import { jl3D,jl2D } from "./jl";
const meshScale = 1/16;

/**
 * 使用主手武器进行攻击
 * @param entity 使用武器的玩家
 * @param wqId 使用的武器ID
 * @param raycast 射线信息
 * @param rotation 视角信息（暂时没用写上就行）
 * @returns 
 */
export async function useZswq(entity:GamePlayerEntity,wqId:number,raycast: GameRaycastResult,rotation: { yaw: number, pitch: number }) {
    if (!entity.canAttack){
        console.warn("error:can't attack!");
        return; 
    } 
    if (wqId===0){
        console.warn("error:don't has zswq!");
        return; 
    } 
    if (jl3D(entity.position,raycast.hitPosition)>20){
        console.warn("error:jl3D is too long!");
        return;
    }
    entity.canAttack = false;
    if (wqId===1){
        console.log(entity.player.name+"...attack! id:"+wqId)
        const jq=world.createEntity({
            mesh: 'mesh/剑气.vb',
            meshScale: new GameVector3(meshScale,meshScale,meshScale),
            position: entity.position,
            collides: false,
            fixed: false,
            gravity: false,
        })

        // 使用标准方向设置方法
        jq.lookAt(
            entity.position.clone().add(new GameVector3(
                Math.cos(rotation.yaw) * Math.cos(rotation.pitch),
                Math.sin(rotation.pitch),
                Math.sin(rotation.yaw) * Math.cos(rotation.pitch)
            )),
            'Y',
            new GameVector3(0, 1, 0)
        );
        const damageInterval = setInterval(() => {
            world.querySelectorAll('.canhurt').forEach(entity => {
                /*
                if (entity !== jq && 
                    entity.position.distanceTo(damageZone.position) < 5) {
                    entity.takeDamage(10); // 假设存在扣血方法
                }
                    */
            });
        }, 500);

        // 5秒后销毁
        setTimeout(() => {
            clearInterval(damageInterval);
            jq.destroy();
            entity.canAttack = true;
        }, 5000);
    }
    else if (wqId === 2){
        console.log(entity.player.name+"...attack! id:"+wqId)
        const jq=world.createEntity({
            mesh: 'mesh/剑气特效.vb',
            meshScale: new GameVector3(meshScale,meshScale,meshScale),
            position: raycast.hitPosition,
            collides: false,
            fixed: false,
            gravity: false,
        })
        const motion = jq.motion.loadByName('attack3s'); 
        //motion.onFinish(()=>{
        //    motion.play();
        //})
        motion.play();
        const damageInterval = setInterval(() => {
            world.querySelectorAll('.canhurt').forEach(e => {
                /*
                if (entity !== jq && 
                    entity.position.distanceTo(damageZone.position) < 5) {
                    entity.takeDamage(10); // 假设存在扣血方法
                }
                */
                let a={x:jq.position.x,z:jq.position.y};
                let b={x:e.position.x,z:e.position.y};
                if (jl2D(a,b)<=2 && Math.abs(jq.position.y-e.position.y)<=6 && e!==entity){
                    e.takeDamage(10,entity);
                }
            });
        }, 500);

        // 3秒后销毁
        setTimeout(() => {
            clearInterval(damageInterval);
            jq.destroy();
            entity.canAttack = true;
        }, 3000);
    }
    else if (wqId === 3){
        console.log(entity.player.name+"...attack! id:"+wqId)
        const jq=world.createEntity({
            mesh: 'mesh/龙卷风特效.vb',
            meshScale: new GameVector3(meshScale,meshScale,meshScale),
            position: raycast.hitPosition,
            collides: false,
            fixed: false,
            gravity: false,
        })
        const motion = jq.motion.loadByName('attack4s'); 
        //motion.onFinish(()=>{
        //    motion.play();
        //})
        motion.play();
        const damageInterval = setInterval(() => {
            world.querySelectorAll('.canhurt').forEach(e => {
                /*
                if (entity !== jq && 
                    entity.position.distanceTo(damageZone.position) < 5) {
                    entity.takeDamage(10); // 假设存在扣血方法
                }
                */
                let a={x:jq.position.x,z:jq.position.y};
                let b={x:e.position.x,z:e.position.y};
                if (jl2D(a,b)<=3 && Math.abs(jq.position.y-e.position.y)<=6 && e!==entity){
                    e.takeDamage(10,entity);
                }
            });
        }, 200);

        // 4秒后销毁
        setTimeout(() => {
            clearInterval(damageInterval);
            jq.destroy();
            entity.canAttack = true;
        }, 4000);
    }
}

/*
// 创建伤害区域模型
function createDamageZone(position: GameVector3, rotation: { yaw: number, pitch: number }) {
    const model = world.createEntity({
        mesh: 'mesh/剑气.vb',
        position: position.clone()
    });

    // 计算前向方向向量
    const forward = new GameVector3(
        Math.cos(rotation.yaw) * Math.cos(rotation.pitch),
        Math.sin(rotation.pitch),
        Math.sin(rotation.yaw) * Math.cos(rotation.pitch)
    );
    
    // 设置模型朝向
    model.lookAt(
        position.clone().add(forward),
        'Y',
        new GameVector3(0, 1, 0)
    );
    return model;
}

// 监听左键按下事件
player.onPress((event) => {
    if (event.button === GameButtonType.ACTION0) {
        // 获取摄像机参数
        const cameraParams = {
            position: event.entity.cameraPosition,
            yaw: event.entity.cameraYaw,
            pitch: event.entity.cameraPitch
        };

        // 创建伤害区域
        const damageZone = createDamageZone(cameraParams.position, {
            yaw: cameraParams.yaw,
            pitch: cameraParams.pitch
        });

        // 设置定时伤害
        const damageInterval = setInterval(() => {
            world.querySelectorAll('*').forEach(entity => {
                if (entity !== damageZone && 
                    entity.position.distanceTo(damageZone.position) < 5) {
                    entity.takeDamage(10); // 假设存在扣血方法
                }
            });
        }, 500);

        // 5秒后销毁
        setTimeout(() => {
            clearInterval(damageInterval);
            damageZone.destroy();
        }, 5000);
    }
});
*/